package com.blt.car.login;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Handler;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.blt.car.MainActivity;
import com.blt.car.R;
import com.blt.car.base.BaseActivity;
import com.blt.car.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

import blt.app_core.api.base.CoreManager;
import blt.app_core.api.login.LoginApi;
import blt.app_core.utils.CommonPref;
import blt.app_core.utils.PreferencesConstant;

/**
 * Created by xgp on 2017/6/11.
 */

public class SplashActivity extends BaseActivity {

    private ImageView loading;
    private ViewPager viewPager;
    private Button toMain;

    private boolean isFrist;
    private Integer[] images = new Integer[]{R.mipmap.guide_1, R.mipmap.guide_2, R.mipmap.guide_3};
    private List<View> mList = new ArrayList<>();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_splash;
    }

    @Override
    protected void initView(ViewHolder holder) {
        loading = holder.get(R.id.splash_load_icon);
        viewPager = holder.get(R.id.splash_viewpager);
        toMain = holder.get(R.id.to_main);
        holder.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(SplashActivity.this, InitActivity.class);
                startActivity(intent);
                finish();
            }
        }, R.id.to_main);

        isFrist = CommonPref.getInstance().getBoolean(PreferencesConstant.FIRST_RUN, false);
        if (!isFrist) {
            showGuide();
        } else {
            showLoading();
        }
    }

    private void showLoading() {
        viewPager.setVisibility(View.GONE);//invisible和gone不同，invisible会隐藏控件，但是控件仍占据空间
        new Handler().postDelayed(new Runnable() {
            @Override
            public void run() {
                boolean isRememberPwd = CommonPref.getInstance().getBoolean(PreferencesConstant.REMEMBER_PASSWORD, false);
                if (isRememberPwd) {
                    String phone = CommonPref.getInstance().getString(PreferencesConstant.PHONE, null);
                    String password = CommonPref.getInstance().getString(PreferencesConstant.PASSWORD, null);
                    CoreManager.getCore(LoginApi.class).login(phone, password);
                    openActivity(MainActivity.class);
                } else {
                    openActivity(InitActivity.class);
                }
                finish();
            }
        }, 2000);

    }

    private void showGuide() {
        CommonPref.getInstance().saveBoolean(PreferencesConstant.FIRST_RUN, true);
        loading.setVisibility(View.GONE);
        viewPager.setOffscreenPageLimit(3);
        SplashViewPagerAdapter adapter = new SplashViewPagerAdapter(mList);
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                toMain.setVisibility(View.GONE);
                if (position == 2) {
                    toMain.setVisibility(View.VISIBLE);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @Override
    protected void initData() {
        super.initData();
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                LinearLayout.LayoutParams.MATCH_PARENT);

        for (int i = 0; i < images.length; i++) {
            ImageView image = new ImageView(this);
            image.setScaleType(ImageView.ScaleType.FIT_XY);
            image.setLayoutParams(lp);
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), images[i]);
            image.setImageBitmap(bitmap);
            mList.add(image);
        }

    }


    private class SplashViewPagerAdapter extends PagerAdapter {

        private List<View> mLists;

        public SplashViewPagerAdapter(List<View> mLists) {
            this.mLists = mLists;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView(mLists.get(position));
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            container.addView(mLists.get(position));
            return mLists.get(position);
        }

        @Override
        public int getCount() {
            return mLists.size();
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }
    }


}
